<?php
namespace Store\Controller;
//use Think\Controller;
use Store\Controller\StoreController;
class CommentController extends StoreController {

    function _initialize() {
        parent::_initialize();
        $this->assign('sys','goods');// 数据集

    }

    //评论管理
    public function index(){
        $this->assign('action','comment');// 页码

        $wxdgoods_comment = M('wxdgoods_comment');

        $datas['store_id'] = session("store_id");

        if(IS_GET){
            if(!empty(I('get.user_name'))){
                $user_name = I('get.user_name');
                $datas['user_name'] = array('like',"%$user_name%");
            }
            if(!empty(I('get.query_start_time'))){
                $query_start_time = strtotime(I('get.query_start_time'));
                $datas['add_time'] = array('egt',$query_start_time);
            }
            if(!empty(I('get.query_end_time'))){
                $query_end_time = strtotime(I('get.query_end_time'));
                $datas['add_time'] = array('elt',$query_end_time);
            }
            if(!empty(I('get.query_end_time')) && !empty(I('get.query_start_time'))){
                $datas['add_time'] = array('between',"$query_start_time,$query_end_time");
            }
        }
        $count = $wxdgoods_comment->where($datas)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $list = $wxdgoods_comment->where($datas)->order('add_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出

        $this->display();
    }


    //商家回复用户
    public function replyComment(){
        $id = I('get.id');
        $wxdgoods_comment = M('wxdgoods_comment');
        if(IS_POST){
            $data['reply'] = I('post.reply');

            $res = $wxdgoods_comment->where("id = $id")->data($data)->save();
            if($res){
                $this->success('操作成功', U('index'),3);die;
            }else{
                $this->error('操作失败');die;
            }
        }
        //商户评价信息
        $comment = $wxdgoods_comment->where("id = $id")->find();
        $this->assign('comment',$comment);

        //订单信息
        $where['order_id'] = $comment['rec_id'];
        $order = D('Order')->relation('OrderCommon')->where($where)->find();
        $this->assign('order',$order);

        //用户信息
        $user = unserialize($order['OrderCommon']['reciver_info']);
        $user['dizhi'] = getCityName($user['province_id'],$user['city_id'],$user['area_id']);
        $this->assign('user',$user);

        //商品信息
        $data['order_id'] = $order['order_id'];
        $data['goods_id'] = $comment['goods_id'];

        $goods = M('order_goods')->where($data)->field('goods_num,goods_image,goods_pay_price')->find();
        $this->assign('goods',$goods);


        $this->assign('id',$id);
        $this->display();
    }

}